n=int(input())
if n>9999 or n<1000:
    print("0")
else:
    a=n//1000
    b=n//100-a*10
    c=n//10-n//100*10
    d=n%10
    k=0
    big=0
    for e in range(1,a+1):
        if (a%e==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    for g in range(1,b+1):
        if (b%g==0):
            k=k+1
    if k==2 and b>big:
        big=b
        k=k-k
    k=k-k
    for f in range(1,c+1):
        if (c%f==0):
            k=k+1
    if k==2 and c>big:
        big=c
        k=k-k
    k=k-k
    for u in range(1,d+1):
        if (d%u==0):
            k=k+1
    if k==2 and d>big:
        big=d
        k=k-k
    k=k-k
    a=n%100
    b=n//100
    c=(n//10)-(n//1000*100)
    for h in range(1,a+1):
        if (a%h==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    for l in range(1,b+1):
        if (b%l==0):
            k=k+1
    if k==2 and b>big:
        big=b
        k=k-k
    k=k-k
    for j in range(1,c+1):
        if (c%j==0):
            k=k+1
    if k==2 and c>big:
        big=c
        k=k-k
    k=k-k
    a=n//10
    b=n%1000
    for m in range(1,a+1):
        if (a%m==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    for x in range(1,b+1):
        if (b%x==0):
            k=k+1
    if k==2 and b>big:
        big=b
        k=k-k
    k=k-k
    a=n
    for v in range(1,a+1):
        if (a%v==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    print(big)

